package com.blue.waffle.iwc.utils;

import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;

/**
 * A manager to handles music operations. Inspired by Gustavo Steigert
 * 
 * "https://code.google.com/p/steigert-libgdx/source/browse/tags/post-20120314/tyrian-game/src/com/blogspot/steigert/tyrian/services/MusicManager.java"
 * 
 * @author Wenbin
 * 
 */
public class MusicManager implements SoundsManager {

	public static final String TAG = "com.blue.waffle.iwc.utils.MusicManager";
	//private static final MusicManager INSTANCE = new MusicManager();

	private IWCGameMusic music;
	private boolean active = true;
	private final AssetManager assetManager;
	/**
	 * libGdx volume value, from 0-1f
	 */
	private float volume = 0.7f;

	/*private MusicManager() {
	}

	public static MusicManager getInstance() {
		return INSTANCE;
	}*/

	public MusicManager(AssetManager manager){
		this.assetManager=manager;
	}
	
	@Override
	public void loadAssets(){
		for (IWCGameMusic music : IWCGameMusic.values()) {
			assetManager.load(music.getFileName(),Music.class);
		}
	}
	
	public enum IWCGameMusic {
		MENU("sounds/menu.mp3");

		private String fileName;
		private Music musicResource;
		
		private IWCGameMusic(String name) {
			this.fileName = name;
		}

		public String getFileName() {
			return fileName;
		}
		
		public Music getMusicResource() {
			return musicResource;
		}
		
		public void setMusicResource(Music musicResource) {
			this.musicResource = musicResource;
		}
	}

	public void play(IWCGameMusic music) {
		
		// Gdx.app.log(TAG, "playing music");
		
		if (this.music==music) {
			return;
		}
		stop();
		Music playMusic= assetManager.get(music.getFileName(), Music.class);
		playMusic.setVolume(volume);
		playMusic.setLooping(true);
		this.music=music;
		this.music.setMusicResource(playMusic);
		if (!active) {
			return;
		}
		playMusic.play();
		
		
	}

	public void play() {

		if (active && music != null) {
			// Gdx.app.log(TAG, "continue playing music");
			music.getMusicResource().play();
		}
	}

	public void pause() {

		if (music != null) {
			// Gdx.app.log(TAG, "pausing music");
			music.getMusicResource().pause();
		}
	}

	public void stop() {

		if (music != null) {
			// Gdx.app.log(TAG, "stoping music");
			music.getMusicResource().stop();
		}
	}

	@Override
	public float getVolume() {
		return volume;
	}

	@Override
	public boolean isActive() {
		return active;
	}

	public void stop(boolean dispose) {

		if (music != null) {
			// Gdx.app.log(TAG, "stoping and disposing music");
			Music playMusic=music.getMusicResource();
			playMusic.stop();
			if (dispose) {
				
				playMusic.dispose();
				playMusic=null;
				this.music.setMusicResource(null);
				this.music = null;
			}
		}
	}

	@Override
	public void setActive(boolean active) {
		this.active = active;
		if (!active) {
			stop();
		} else {
			play();
		}
	}

	@Override
	public void setVolume(float volume) {

		if (volume < 0 || volume > 1) {
			throw new IllegalArgumentException(
					"Volume can only set from 0 - 1.");
		}
		// Gdx.app.log(TAG, "setting volume to "+volume);
		this.volume = volume;
		if (music != null) {
			music.getMusicResource().setVolume(volume);
		}
	}

	@Override
	public void dispose() {
		// Gdx.app.log(TAG, "disposing");
		stop(true);
	}

}
